package gh625simpleviewer;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreeSelectionModel;
import org.jdesktop.swingx.JXMapKit.DefaultProviders;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.WaypointPainter;

public class MainFrame extends javax.swing.JFrame {

    private TreeSelectionModel tsm = new DefaultTreeSelectionModel();
    private ArrayList<TrackPoint> tplist = new ArrayList<TrackPoint>();
    private int trackmode = 0;

    public MainFrame() {
        initComponents();
        RefreshTree();
        tsm.setSelectionMode(javax.swing.tree.DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree1.setSelectionModel(tsm);

        jXMapKit1.setDefaultProvider(DefaultProviders.OpenStreetMaps);
        jXMapKit1.setMiniMapVisible(false);
        jXMapKit1.setZoomButtonsVisible(false);

        WaypointPainter painter = new WaypointPainter() {

            @Override
            protected void doPaint(Graphics2D g, JXMapViewer map, int width, int height) {
                if (tplist.size() > 0) {
                    TrackPoint tp0 = tplist.get(0);
                    double lat0 = (double) tp0.Lat / 1000000;
                    double lon0 = (double) tp0.Lon / 1000000;
                    Point2D po0 = map.convertGeoPositionToPoint(new GeoPosition(lat0, lon0));
                    int x0 = (int) po0.getX();
                    int y0 = (int) po0.getY();
                    for (TrackPoint tp : tplist) {
                        double lat = (double) tp.Lat / 1000000;
                        double lon = (double) tp.Lon / 1000000;
                        Point2D po = map.convertGeoPositionToPoint(new GeoPosition(lat, lon));
                        switch (trackmode) {
                            case 0:
                                g.setColor(Color.BLUE);
                                break;
                            case 1:
                                if (tp.Speed > 2500) {
                                    g.setColor(Color.RED);
                                } else {
                                    g.setColor(Color.BLUE);
                                }
                                break;
                            case 2:
                                if (tp.HeartRate > 155) {
                                    g.setColor(Color.RED);
                                } else {
                                    g.setColor(Color.BLUE);
                                }
                                break;
                        }
                        g.fillOval(x0 - 2, y0 - 2, 4, 4);
                        g.drawLine(x0, y0, (int) po.getX(), (int) po.getY());
                        x0 = (int) po.getX();
                        y0 = (int) po.getY();
                    }
                }
            }
        };

        jXMapKit1.getMainMap().setOverlayPainter(painter);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        jMenuItem10 = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jXMapKit1 = new org.jdesktop.swingx.JXMapKit();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();

        jMenuItem7.setText("Export lap...");
        jPopupMenu1.add(jMenuItem7);

        jMenuItem10.setText("Export track...");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem10);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(Vars.Version);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("1.07.2009");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("14:55");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("19:30");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("3.07.2009");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("15:05");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("4.07.2009");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("18:20");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("5.07.2009");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("20:10");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("7.07.2009");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("9:15");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.setSelectionModel(null);
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jPanel1.setLayout(new java.awt.CardLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("No image");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.add(jPanel2, "card2");

        jLabel2.setText("Date:");

        jLabel3.setText("Tracks:");

        jLabel4.setText("Distance:");

        jLabel5.setText("Duration:");

        jLabel6.setText("Calories:");

        jLabel9.setText("Day information");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addContainerGap(398, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addContainerGap(431, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel3, "card3");

        jLabel8.setText("Track information");
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        jXMapKit1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel10.setText("Track information");
        jLabel10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel10MouseClicked(evt);
            }
        });

        jLabel11.setText("Track information");
        jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel11MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXMapKit1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXMapKit1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel4, "card4");

        jLabel7.setText("Lap information");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addContainerGap(400, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addContainerGap(548, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel5, "card5");

        jMenu1.setText("File");

        jMenuItem4.setText("Get tracks from device...");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);
        jMenu1.add(jSeparator1);

        jMenuItem1.setText("Import tracks...");
        jMenuItem1.setEnabled(false);
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Export tracks...");
        jMenuItem2.setEnabled(false);
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator2);

        jMenuItem3.setText("Exit");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu4.setText("Tools");

        jMenuItem6.setText("Options...");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem6);

        jMenuBar1.add(jMenu4);

        jMenu3.setText("Help");

        jMenuItem5.setText("About");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem5);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void RefreshTree() {
        Vars.LoadTracks();
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("*");
        for (OneDay od : Vars.days) {
            DefaultMutableTreeNode daynode = new DefaultMutableTreeNode(od);
            for (Track trk : od.DayTracks) {
                DefaultMutableTreeNode trknode = new DefaultMutableTreeNode(trk);
                for (TrackLap tl : trk.tls) {
                    trknode.add(new DefaultMutableTreeNode(tl));
                }
                daynode.add(trknode);
            }
            top.add(daynode);
        }
        DefaultTreeModel tm = new DefaultTreeModel(top);
        jTree1.setModel(tm);
    }

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        dispose();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        GetTracksDialog dlg = new GetTracksDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTree();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        ImportTracks dlg = new ImportTracks(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        ExportTracks dlg = new ExportTracks(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        OptionsDialog dlg = new OptionsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        JOptionPane.showMessageDialog(null, Vars.Version, "About", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON2 || evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            jTree1.setSelectionPath(jTree1.getPathForLocation(evt.getX(), evt.getY()));
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        if (node == null || node.isRoot()) {
            return;
        }
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON2 || evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            if (node.isLeaf()) {
                //jPopupMenu1.show(jTree1, evt.getX(), evt.getY());
                return;
            }
            if (node.getFirstChild().isLeaf()) {
                jPopupMenu2.show(jTree1, evt.getX(), evt.getY());
                return;
            }
        }
    }//GEN-LAST:event_jTree1MouseClicked

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        JFileChooser jfc = new JFileChooser();
        String filen = jTree1.getLastSelectedPathComponent().toString();
        filen = filen.replaceAll(": ", "_");
        filen = filen.replaceAll(":", "-");
        filen = filen.replaceAll(" ", "_");
        jfc.setSelectedFile(new File(filen));
        jfc.setAcceptAllFileFilterUsed(false);
        //jfc.addChoosableFileFilter(new FileNameExtensionFilter("GPX file", "gpx"));
        jfc.addChoosableFileFilter(new FileNameExtensionFilter("KML file", "kml"));
        if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            if (jfc.getFileFilter().toString().contains("gpx")) {
                filen = jfc.getSelectedFile().getPath() + ".gpx";
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
                Track trk = (Track) node.getUserObject();
                Vars.ExportTrackToGPX(trk, filen);
            }
            if (jfc.getFileFilter().toString().contains("kml")) {
                filen = jfc.getSelectedFile().getPath() + ".kml";
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
                Track trk = (Track) node.getUserObject();
                Vars.ExportTrackToKML(trk, filen);
            }
        }
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        CardLayout cl = (CardLayout) jPanel1.getLayout();
        if (node == null || node.isRoot()) {
            cl.show(jPanel1, "card2");
            return;
        }
        if (node.getUserObject().getClass() == OneDay.class) {
            cl.show(jPanel1, "card3");
            OneDay od = (OneDay) node.getUserObject();
            jLabel2.setText(od.toString());
            jLabel3.setText("Tracks: " + od.DayTracks.size());
            jLabel4.setText("Distance: " + (od.Distance / 1000) + "," + (od.Distance - (od.Distance / 1000) * 1000) + " Km");
            jLabel5.setText("Duration: " + od.getDurationString());
            jLabel6.setText("Calories: " + od.Calories + " Cal");
            return;
        }
        if (node.getUserObject().getClass() == Track.class) {
            cl.show(jPanel1, "card4");
            Track trk = (Track) node.getUserObject();

            tplist.clear();
            double maxlat = 0, minlat = 90, maxlon = 0, minlon = 90;
            for (TrackPoint tp : trk.tps) {
                double lat = (double) tp.Lat / 1000000;
                double lon = (double) tp.Lon / 1000000;
                if (lat > maxlat) {
                    maxlat = lat;
                }
                if (lat < minlat) {
                    minlat = lat;
                }
                if (lon > maxlon) {
                    maxlon = lon;
                }
                if (lon < minlon) {
                    minlon = lon;
                }
                tplist.add(tp);
            }

            double lat = (minlat + maxlat) / 2;
            double lon = (minlon + maxlon) / 2;
            jXMapKit1.setAddressLocation(new GeoPosition(lat, lon));

            for (int zl = 1; zl <= 15; zl++) {
                jXMapKit1.setZoom(zl);
                int xmin = (int) jXMapKit1.getMainMap().convertGeoPositionToPoint(new GeoPosition(minlat, minlon)).getX();
                int ymin = (int) jXMapKit1.getMainMap().convertGeoPositionToPoint(new GeoPosition(minlat, minlon)).getY();
                int xmax = (int) jXMapKit1.getMainMap().convertGeoPositionToPoint(new GeoPosition(maxlat, maxlon)).getX();
                int ymax = (int) jXMapKit1.getMainMap().convertGeoPositionToPoint(new GeoPosition(maxlat, maxlon)).getY();
                int gmwidth = jXMapKit1.getMainMap().getViewportBounds().width;
                int gmheight = jXMapKit1.getMainMap().getViewportBounds().height;
                if (xmin > 0 && ymax > 0 && xmax < gmwidth && ymin < gmheight) {
                    break;
                }
            }

            jLabel8.setText("<html>" + trk.toString() + ", duration: <font color=#00008B>" + trk.getTrackDurationString() + "</font></html>");
            jLabel10.setText("<html>Distance: <font color=#00008B>" + (trk.Distance / 1000) + "," + (trk.Distance - (trk.Distance / 1000) * 1000) + "</font> Km, average speed: <font color=#00008B>" + trk.AvgSpeed + "</font>, top speed: <font color=#00008B>" + trk.TopSpeed + "</font></html>");
            jLabel11.setText("<html>Average heart rate: <font color=#00008B>" + trk.AvgHeartBeat + "</font>, top heart rate: <font color=#00008B>" + trk.TopHeartBeat + "</font>, calories: <font color=#00008B>" + trk.Calories + "</font></html>");

            trackmode = 0;

            return;
        }
        if (node.getUserObject().getClass() == TrackLap.class) {
            cl.show(jPanel1, "card5");
            TrackLap lap = (TrackLap) node.getUserObject();
            jLabel7.setText("Lap information: " + lap.toString());
            return;
        }
        cl.show(jPanel1, "card2");
    }//GEN-LAST:event_jTree1ValueChanged

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        trackmode = 0;
        jXMapKit1.repaint();
    }//GEN-LAST:event_jLabel8MouseClicked

    private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseClicked
        trackmode = 1;
        jXMapKit1.repaint();
    }//GEN-LAST:event_jLabel10MouseClicked

    private void jLabel11MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel11MouseClicked
        trackmode = 2;
        jXMapKit1.repaint();
    }//GEN-LAST:event_jLabel11MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTree jTree1;
    private org.jdesktop.swingx.JXMapKit jXMapKit1;
    // End of variables declaration//GEN-END:variables
}
